package com.zyzy.leftfourdead.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.adapter.HomePageFragmentAdapter;
import com.zyzy.leftfourdead.fragment.homepager.AutumnFestivalFragment;
import com.zyzy.leftfourdead.fragment.homepager.DairyFoodFragment;
import com.zyzy.leftfourdead.fragment.homepager.DrinkFragment;
import com.zyzy.leftfourdead.fragment.homepager.FruitFragment;
import com.zyzy.leftfourdead.fragment.homepager.MeatEggFragment;
import com.zyzy.leftfourdead.fragment.homepager.SeckillFragment;
import com.zyzy.leftfourdead.fragment.homepager.SnacksFragment;
import com.zyzy.leftfourdead.fragment.homepager.WaterFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by loverforever on 2016/8/20.
 * 首页大碎片
 */
public class HomePageFragment extends Fragment {
    //上下文对象
    private Context context;
    //定义控件
    private TabLayout tabLayout_home;
    private ViewPager viewPager_home;
    //    定义碎片(8个)
    private WaterFragment waterFragment;
    private SnacksFragment snacksFragment;
    private SeckillFragment seckillFragment;
    private MeatEggFragment meatEggFragment;
    private FruitFragment fruitFragment;
    private DrinkFragment drinkFragment;
    private DairyFoodFragment dairyFoodFragment;
    private AutumnFestivalFragment autumnFestivalFragment;
    //碎片管理器
    private FragmentManager manager;
    //tablayout数据源
    private String[] arrTitles;
    //碎片集合
    private List<Fragment> list = new ArrayList<>();
    //定义适配器
    private HomePageFragmentAdapter adapter;


    //无参的构造方法
    public HomePageFragment(){

    }

    public void setManager(FragmentManager manager){
        this.manager = manager;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化上下文对象
        context = getContext();
        //获取tablayout 数据源
        arrTitles = getResources().getStringArray(R.array.arrTitles);
        initData();
    }


    private void initData() {
        waterFragment = new WaterFragment();
        seckillFragment = new SeckillFragment();
        autumnFestivalFragment = new AutumnFestivalFragment();
        fruitFragment = new FruitFragment();
        meatEggFragment = new MeatEggFragment();
        dairyFoodFragment = new DairyFoodFragment();
        snacksFragment = new SnacksFragment();
        drinkFragment = new DrinkFragment();


        //初始化碎片
        list.add(waterFragment);
        list.add(seckillFragment);
        list.add(autumnFestivalFragment);
        list.add(fruitFragment);
        list.add(meatEggFragment);
        list.add(dairyFoodFragment);
        list.add(snacksFragment);
        list.add(drinkFragment);
//        碎片加入list集合
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_homepage, container, false);
        //          初始化view
        tabLayout_home = (TabLayout) view.findViewById(R.id.tabLayout_home);
        //        绑定TabLayout
        viewPager_home = (ViewPager) view.findViewById(R.id.viewPager_home);
        //        绑定ViewPager
        viewPager_home.setOffscreenPageLimit(4);
        //manager = getFragmentManager();
        //        初始化碎片管理器
        adapter = new HomePageFragmentAdapter(manager, list, arrTitles);
        //        初始化适配器
        viewPager_home.setAdapter(adapter);
        //        绑定适配器
        tabLayout_home.setupWithViewPager(viewPager_home);
        //Tablayout和viewpager关联
        return view;
    }

   /* @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }*/
}
